import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Passage extends javax.swing.JFrame implements ActionListener
{

	static PrintWriter out;
	static FileReader reader = null;

	{
		//Set Look & Feel
		try
		{
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private JLabel From;
	private JLabel To;
	private JTextArea TimeTable;
	private JComboBox timeMin;
	private JLabel colon;
	private JComboBox timeHours;
	private JLabel Time;
	private JTextField DateField;
	private JLabel Date;
	private JComboBox selectAreaFrom;
	private JComboBox selectAreaTo;
	private JComboBox toBusStation;
	private JComboBox toArea;
	private JComboBox fromBusStation;
	private JComboBox fromArea;
	private JScrollPane jScrollPane1;
	private JButton Timetable;
	private JButton Search;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				Passage inst = new Passage();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Passage()
	{
		super();
		initGUI();
	}

	private void initGUI()
	{
		try
		{
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				From = new JLabel();
				getContentPane().add(From, "Center");
				From.setText("From");
				From.setBounds(7, 7, 71, 23);
			}
			{

				ComboBoxModel fromAreaModel = new DefaultComboBoxModel(new String[] {
						"Select Area", "Birch Vale", "Compstall", "Hayfield",
						"Low Leighton", "Marple", "New Mills", "Newtown", "Romiley",
						"Stockport", "Strines", "Thornsett" });
				//"Glossop", "Offerton",
				fromArea = new JComboBox();
				getContentPane().add(fromArea);
				fromArea.setModel(fromAreaModel);
				fromArea.setBounds(46, 10, 246, 18);
				fromArea.setActionCommand("from");
				fromArea.addActionListener(this);
			}
			{
				ComboBoxModel selectAreaFromModel = new DefaultComboBoxModel(
						new String[] { "Select Bus Stop" });
				selectAreaFrom = new JComboBox();
				getContentPane().add(selectAreaFrom);
				selectAreaFrom.setModel(selectAreaFromModel);
				selectAreaFrom.setBounds(303, 10, 246, 18);
				selectAreaFrom.setActionCommand("fromBs");
				selectAreaFrom.addActionListener(this);
			}
			{
				To = new JLabel();
				getContentPane().add(To);
				To.setText("To");
				To.setBounds(7, 31, 71, 23);
			}
			{
				ComboBoxModel toAreaModel = new DefaultComboBoxModel(new String[] {
						"Select Area", "Birch Vale", "Hayfield", "Low Leighton", "Marple",
						"New Mills", "Newtown", "Offerton", "Romiley", "Stockport",
						"Strines" });
				//"Glossop","Compstall","Thornsett"
				toArea = new JComboBox();
				getContentPane().add(toArea);
				toArea.setModel(toAreaModel);
				toArea.setBounds(46, 34, 246, 18);
				toArea.setActionCommand("to");
				toArea.addActionListener(this);
			}
			{
				ComboBoxModel selectAreaToModel = new DefaultComboBoxModel(
						new String[] { "Select Bus Stop" });
				selectAreaTo = new JComboBox();
				getContentPane().add(selectAreaTo);
				selectAreaTo.setModel(selectAreaToModel);
				selectAreaTo.setBounds(303, 34, 246, 18);
				selectAreaTo.setActionCommand("toBs");
				selectAreaTo.addActionListener(this);

			}
			{
				Date = new JLabel();
				getContentPane().add(Date);
				Date.setText("Date");
				Date.setBounds(7, 65, 71, 23);
			}
			{
				DateField = new JTextField();
				getContentPane().add(DateField);
				DateField.setBounds(45, 66, 92, 23);
				DateField.setText("12/05/2011");
				DateField.setHorizontalAlignment(JTextField.CENTER);
			}
			{
				Time = new JLabel();
				getContentPane().add(Time);
				Time.setText("Time");
				Time.setBounds(147, 66, 71, 23);
			}
			{
				Search = new JButton();
				getContentPane().add(Search);
				Search.setText("Search");
				Search.setBounds(315, 65, 65, 22);
				Search.setActionCommand("Search");
				Search.addActionListener(this);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Real time");
				jButton1.setActionCommand("realTime");
				jButton1.setBounds(384, 65, 81, 22);
				jButton1.addActionListener(this);
			}
			{
				Timetable = new JButton();
				getContentPane().add(Timetable);
				Timetable.setText("Timetable");
				Timetable.setBounds(468, 65, 82, 22);
				Timetable.addActionListener(this);
			}
			{
				ComboBoxModel timeHoursModel = new DefaultComboBoxModel(new String[] {
						"08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
						"19", "20", "21", "22", "23", "00", "01", "02", "03", "04", "05",
						"06", "07" });
				timeHours = new JComboBox();
				getContentPane().add(timeHours);
				timeHours.setModel(timeHoursModel);
				timeHours.setBounds(188, 68, 50, 18);
			}
			{
				colon = new JLabel();
				getContentPane().add(colon);
				colon.setText(":");
				colon.setBounds(243, 69, 21, 16);
			}
			{
				ComboBoxModel timeMinModel = new DefaultComboBoxModel(new String[] {
						"00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "55",
						"60" });
				timeMin = new JComboBox();
				getContentPane().add(timeMin);
				timeMin.setModel(timeMinModel);
				timeMin.setBounds(253, 68, 50, 18);
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(7, 94, 543, 107);
				{
					TimeTable = new JTextArea();
					jScrollPane1.setViewportView(TimeTable);
					TimeTable.setBounds(7, 94, 543, 107);
					TimeTable.setPreferredSize(new java.awt.Dimension(600, 160));
				}
			}

			pack();
			this.setSize(557, 230);
		}
		catch (Exception e)
		{
			//add your error handling code here
			e.printStackTrace();
		}

	}

	String areaFromName;
	String stationFromName;

	String areaToName;
	String stationToName;

	Date Date1;
	private JButton jButton1;
	String hours;
	String mins;

	boolean fromBusStationT = false;
	boolean toBusStationT = false;

	public void actionPerformed(ActionEvent event)
	{
		String s = event.getActionCommand();

		if (s.equals("from"))
		{
			if (fromBusStationT == true)
			{
				getContentPane().remove(fromBusStation);
			}
			JComboBox areaFrom = (JComboBox) event.getSource();
			areaFromName = (String) areaFrom.getSelectedItem();
			String[] fromBusStops = NameInfo.getBusStopsNames(areaFromName);
			getContentPane().remove(selectAreaFrom);
			ComboBoxModel fromBusStationModel = new DefaultComboBoxModel(
					new String[] { "Select Bus Stop" });
			fromBusStation = new JComboBox();
			getContentPane().add(fromBusStation);
			fromBusStationT = true;
			fromBusStation.setModel(fromBusStationModel);
			fromBusStation.setBounds(303, 10, 246, 18);
			if (fromBusStops != null)
			{

				for (int i = 0; i < fromBusStops.length; i++)
				{
					fromBusStation.addItem(fromBusStops[i]);
				}
			}

			this.repaint();
		}

		/*
		 * if (s.equals("fromBs")) { JComboBox stationFrom = (JComboBox)
		 * event.getSource(); stationFromName = (String)
		 * stationFrom.getSelectedItem(); System.out.print(areaFromName + "  ");
		 * System.out.println(stationFromName); }
		 */

		if (s.equals("to"))
		{
			if (toBusStationT == true)
			{
				getContentPane().remove(toBusStation);
			}
			JComboBox areaTo = (JComboBox) event.getSource();
			areaToName = (String) areaTo.getSelectedItem();
			String[] toBusStops = NameInfo.getBusStopsNames(areaToName);
			getContentPane().remove(selectAreaTo);
			ComboBoxModel toBusStationModel = new DefaultComboBoxModel(
					new String[] { "Select Bus Stop" });
			toBusStation = new JComboBox();
			getContentPane().add(toBusStation);
			toBusStationT = true;
			toBusStation.setModel(toBusStationModel);
			toBusStation.setBounds(303, 34, 246, 18);
			if (toBusStops != null)
			{
				for (int i = 0; i < toBusStops.length; i++)
				{
					toBusStation.addItem(toBusStops[i]);
				}
			}
			this.repaint();

		}

		/*
		 * if (s.equals("toBs")) { JComboBox stationTo = (JComboBox)
		 * event.getSource(); stationToName = (String) stationTo.getSelectedItem();
		 * System.out.print(areaToName + "  "); System.out.println(stationToName); }
		 */
		if (s.equals("Search"))
		{
			String ffArea = (String) fromArea.getSelectedItem();
			String ttArea = (String) toArea.getSelectedItem();
			if (ffArea.equals("Select Area") || ttArea.equals("Select Area"))
			{
				Object[] options = { "OK" };
				JOptionPane.showOptionDialog(null, "Please select the Area", "Warning",
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
						options, options[0]);
			}
			else
			{
				stationFromName = (String) fromBusStation.getSelectedItem();
				stationToName = (String) toBusStation.getSelectedItem();
				if (stationFromName.equals("Select Bus Stop")
						|| stationToName.equals("Select Bus Stop"))
				{
					Object[] options = { "OK" };
					JOptionPane.showOptionDialog(null, "Please select the Bus Stop",
							"Warning", JOptionPane.DEFAULT_OPTION,
							JOptionPane.WARNING_MESSAGE, null, options, options[0]);
				}
				else
				{
					hours = (String) timeHours.getSelectedItem();
					mins = (String) timeMin.getSelectedItem();
					int intHours = Integer.parseInt(hours);
					int intMins = Integer.parseInt(mins);

					String date = DateField.getText();
					boolean d = isValidDate(date);

					if (d == false)
					{
						Object[] options = { "OK" };
						JOptionPane.showOptionDialog(null,
								"The date that you provided is invalid", "Warning",
								JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
								options, options[0]);
					}
					else
					{
						try
						{
							DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
							Date1 = df.parse(date);

						}
						catch (ParseException e)
						{
							System.err.println("Caught IOException: " + e.getMessage());
						}

						CLI.testPassengerChoice(areaFromName, areaToName, stationFromName,
								stationToName, Date1, DateValidation.formatChosenTimeToInt(
										intHours, intMins));
						try
						{
							reader = new FileReader("passengerResult.txt");
							TimeTable.read(reader, "passengerResult.txt");
						}
						catch (IOException exception)
						{
							System.err.println("Load oops");
							exception.printStackTrace();
						}
						finally
						{
							if (reader != null)
							{
								try
								{
									reader.close();
								}
								catch (IOException exception)
								{
									System.err.println("Error closing reader");
									exception.printStackTrace();
								}
							}
						}

						System.out.println(areaFromName + " " + stationFromName);
						System.out.println(areaToName + " " + stationToName);
						System.out.println(hours + ":" + mins);
						System.out.println(Date1);

					}
				}
			}
		}
		
		if (s.equals("jButton1"))
		{
			String ffArea = (String) fromArea.getSelectedItem();
			String ttArea = (String) toArea.getSelectedItem();
			if (ffArea.equals("Select Area") || ttArea.equals("Select Area"))
			{
				Object[] options = { "OK" };
				JOptionPane.showOptionDialog(null, "Please select the Area", "Warning",
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
						options, options[0]);
			}
			else
			{
				stationFromName = (String) fromBusStation.getSelectedItem();
				stationToName = (String) toBusStation.getSelectedItem();
				if (stationFromName.equals("Select Bus Stop")
						|| stationToName.equals("Select Bus Stop"))
				{
					Object[] options = { "OK" };
					JOptionPane.showOptionDialog(null, "Please select the Bus Stop",
							"Warning", JOptionPane.DEFAULT_OPTION,
							JOptionPane.WARNING_MESSAGE, null, options, options[0]);
				}
				else
				{
					hours = (String) timeHours.getSelectedItem();
					mins = (String) timeMin.getSelectedItem();
					int intHours = Integer.parseInt(hours);
					int intMins = Integer.parseInt(mins);

					String date = DateField.getText();
					boolean d = isValidDate(date);

					if (d == false)
					{
						Object[] options = { "OK" };
						JOptionPane.showOptionDialog(null,
								"The date that you provided is invalid", "Warning",
								JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
								options, options[0]);
					}
					else
					{
						try
						{
							DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
							Date1 = df.parse(date);

						}
						catch (ParseException e)
						{
							System.err.println("Caught IOException: " + e.getMessage());
						}

						RealTime.checkInfo(areaFromName, areaToName, stationFromName,
								stationToName, Date1, DateValidation.formatChosenTimeToInt(
										intHours, intMins));
						try
						{
							reader = new FileReader("RealTime.txt");
							TimeTable.read(reader, "RealTime.txt");
						}
						catch (IOException exception)
						{
							System.err.println("Load oops");
							exception.printStackTrace();
						}
						finally
						{
							if (reader != null)
							{
								try
								{
									reader.close();
								}
								catch (IOException exception)
								{
									System.err.println("Error closing reader");
									exception.printStackTrace();
								}
							}
						}

						System.out.println(areaFromName + " " + stationFromName);
						System.out.println(areaToName + " " + stationToName);
						System.out.println(hours + ":" + mins);
						System.out.println(Date1);

					}
				}
			}
		}
		
		if (event.getSource() == Timetable)
		{
			new PassageTimetable().setVisible(true);
		}

	}

	public boolean isValidDate(String date)
	{
		// set date format dd/MM/yyyy
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		// testDate variable
		Date testDate = null;

		// to parse the string into date form
		try
		{

			testDate = sdf.parse(date);

		}

		// if the format of the string provided doesn't match the format we
		// declared in SimpleDateFormat() we will get an exception

		catch (ParseException e)
		{
			return false;
		}

		// dateformat.parse will accept any date as long as it's in the format
		// you defined, it simply rolls dates over, for example, december 32
		// becomes jan 1 and december 0 becomes november 30
		// This statement will make sure that once the string
		// has been checked for proper formatting that the date is still the
		// date that was entered, if it's not, we assume that the date is invalid

		if (!sdf.format(testDate).equals(date))
		{
			return false;
		}
		else
			return true;

	} // end isValidDate
}
